

    // 加入购物车
    let state = ''
    $('.allgoods').on('click', '.buy', function (e) {
    if(!token){
      cocoMessage.error('请先完成登录');
      // swal({title:'请先完成登录',text: "美团提示!",icon: "error",button: "确定!",});
      return
    }
    let goodsId = $(this).parents('.goods').attr('id')
    $.post('/api/carts/create.jsp',{
      token,
      buyNum:1,
      goodsId
    },res=>{
      console.log(res);
      state = res.meta.state
      if(res.meta.state!=201){
        cocoMessage.error(res.meta.msg);
        // swal({title:res.meta.msg,text: "美团提示!",icon: "error",button: "确定!",});
        // return
      }
    },'json')
  })
  // 收集数据


// ###===============================
// 外卖详情页 门店 和商品信息
let store_id = getQueryVariable('store_id')
$.get('/api/waimai/goods.jsp',{
  store_id
},res=>{
  
  if(res.meta.state==200){
    //门店信息替换
    $('.store .name').text(res.data.store.title)
    $('.address>p:eq(0)').text(`地址：${res.data.store.address}`)
    $('.address>p:eq(1)').text(`电话：${res.data.store.tel}`)
    $('.address>p:eq(2)').text(`营业时间：${res.data.store.work_time}`)
    $('.big .imgbox img').attr('src','http://tmp00001.zhaodashen.cn/'+res.data.store.img)
    let html = ''
    $('.d-right>ul').html('')
    $.each(res.data.store.imgs.split(','),(index,item)=>{
      html+=`
      <li>
        <div class="imgbox" style="height: 100%; width: 100%;"><img
            src='http://tmp00001.zhaodashen.cn/${item}'>
        </div>
      </li>
      `
    })
    $('.d-right>ul').html(html)
    // 门店商品信息替换
    $('.allgoods').html('')
    let goodsHtml = ''
    $.each(res.data.goods,(index,item)=>{
      goodsHtml+=`
      <div class="goods" id="${item.goods_id}">
        <div class="one clear">
          <div class="pic">
            <div class="imgbox">
              <img
                src="http://tmp00001.zhaodashen.cn/${item.goods_img}">
            </div>
          </div>
          <div class="info">
            <a class="fr buy" href="javascript:;">加入购物车</a>
            <p class="name"><span>${item.goods_name}${item.goods_name_style}</span></p>
            <p class="price">
              <b>￥</b>
              <font>${item.shop_price}</font>
              <span>门市价￥${item.market_price}</span>
            </p>
          </div>
        </div>
        <div class="line"></div>
      </div>
      `
    })
    $('.allgoods').html(goodsHtml)
  }else{
    cocoMessage.error(res.meta.msg);
    // swal({title:res.meta.msg,text: "美团提示!",icon: "error",button: "确定!",});
    setTimeout(()=>{
      location.href = './index.html'
    },2000)
  }
},'json')

$.get('/api/carts/store.jsp',{store_id,token},res=>{
    console.log(res);
    $('.mofakoudai').text('')
    let html = ''
    $.each(res.data,(index,item)=>{
      //注意每一个li标签需要有goods_id 否则刷新后 加入购物车不能累计
    html+=`
      <li class='goods${item.goods_id}'>
        <div class="pic">
          <div class="imgbox">
            <img
              src='http://tmp00001.zhaodashen.cn/${item.imgs.goods_img}'>
          </div>
        </div>
        <p class="name">${item.goods_name}</p>
        <p class="desc">门市价<del>￥${item.market_price}</del></p>
        <p class="price"><b>￥</b>${item.shop_price}<button>已购<font>${item.goods_number}</font></button></p>
      </li>
    `   
    })
    $('.mofakoudai').html(html)
  },'json')


  // $(function () {
    // ### 全部变量
 
    // ### 和后端交互

    // ### 门店放大镜切换
    $('.store .d-right ul').on('click', 'li', function () {
      $('.big .imgbox img').attr('src', $(this).find('img').attr('src'))
  })





  // ### 飞入购物车 start
  $('.allgoods').on('click', '.buy', function (e) {
      // ####======================================================
      let token = localStorage.getItem('token') || sessionStorage.getItem('token')
      if(!token) return
      if(state==500) return
      let endObj = document.querySelector('.guess-you-like h4')
      let goods_img = $(this)
          .parents('.goods')
          .find('.imgbox img')
          .attr('src')
      let goods_name = $(this).parents('.goods').find('.name span').text()
      let goods_price = $(this).parents('.goods').find('.price font').text()
      let goods_price2 = $(this).parents('.goods').find('.price span').text()
      let goods_id = $(this).parents('.goods').attr('id')
      // 飞图片
      let flyer = $(`<img class="flyer" src="${goods_img}"/>`)
      // 起飞
      flyer.fly({
          start: {
              left: e.clientX - 788,
              top: e.clientY,
          },
          end: {
              left: endObj.offsetLeft,
              top: endObj.offsetTop - document.documentElement.scrollTop,
              width: 20,
              height: 20,
          },
          onEnd: () => {
              // alert('ok')
              $('.flyer').remove()

              if ($('.mofakoudai').find(`.goods${goods_id}`).length) {
                  // 累计+1
                  console.log(1)
                  let data = $('.mofakoudai')
                      .find(`.goods${goods_id} button font`) //新建的li标签需要有goods_id 起到累计的作用
                      .text()
                  $('.mofakoudai')
                      .find(`.goods${goods_id} button font`)
                      .text(parseInt(data) + 1)
              } else {
                  // 新增
                  console.log(1)
                  $('.mofakoudai').prepend(`
                <li class="goods${goods_id}">
                    <div class="pic">
                        <div class="imgbox">
                            <img src="${goods_img}">
                        </div>
                    </div>
                    <p class="name">${goods_name}</p>
                    <p class="desc">门市价<del>￥${goods_price2}</del></p>
                    <p class="price"><b>￥</b>${goods_price} <button>已购<font>1</font></button></p>
                </li>
            `)
              }
          },
      })
  })
  // 飞入购物车 end
// })